/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_MagSen.h
 *
 *  Created on: 2023年2月17日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_MAGSEN_H_
#define PERIPHERAL_LAYER_INC_LOS_MAGSEN_H_

#include "LOS_SYS.h"

#define MAGSEN_MAXNUM LOS_MAX14830_NUM1
#define MAGSEN_MAXPort 1

typedef struct LOS_MAGMESTA_t
{
    /* 健康管理状态量 */
    uint8_t VAL;                 //数据有效性标志，0：有效， 其他值：无效
    uint8_t CMD;                 //开关机指令状态,发送开机指令后置1，发送关机指令后置0
    uint8_t PWR;                 //电源状态，读取电源遥测，通电为1，断电为0
    uint8_t MSG;                 //通信状态，通信正常为0，通信异常为其他值
    uint8_t SWNUM;
    uint8_t NFR;                 //Need For Reset 需要复位标志位，1为需要复位，0为不需要
    uint8_t RBT_F;               //表示单机是否处于重启过程中 0：未处于 1：处于重启过程中
    uint8_t RECOV;               //恢复计数：单机重启完成后，需要等待一段时间才能再次进入重启流程，将该计数置于某一值开始倒计时，倒计时结束后会再次开始判断是否需要重启

    uint8_t RBCNT;               //复位次数
    uint8_t HL_TXF;              //HL_前缀表示健康管理相关变量。遥测请求发送完成标志,发送一次后该量置1，接收到回应后置0(TX FINISHED)
    uint8_t HL_WCNT;             //等待接收计数器。该计数器在操作系统的一个线程中累加，当HL_TXF为1时该值+1，HL_TXF为0时该值清0，当累加到一定值时系统会判断通信异常，并将MSG置1
    uint8_t First_Run;           //首次运行标志，用于判断是否需要初始化
    uint32_t TMCNT;
    uint16_t FlagF_CNT;         //物理量异常重启计数
    uint16_t FlagT_CNT;         //通信异常重启计数
    uint16_t FlagY_CNT;    
    /* 磁强计工程参数 */
    double Field[3];             //磁感应强度，单位特斯拉（磁强计输出为高斯，在处理数据时转换为特斯拉）
    uint16_t odata[3];
    uint8_t Flag_D;              //AOCS的FlagD
    float AfterComp[3];          //磁强计补偿后的值
}LOS_MAGMESTA_t;

typedef struct LOS_MAGMETEST_t
{
    uint8_t axis_sel;  //磁感应强度上注轴选择  0:不改变/恢复  1:X轴  2:Y轴  3:Z轴  4:开始作用
    float x_upt;     
    float y_upt;     
    float z_upt;     
}LOS_MAGMETEST_t;

extern LOS_MAGMESTA_t STA_MAGME;
LOS_MAGMETEST_t TEST_MAGME;

extern uint8_t TMPACK_MAGME[17];

void LOS_MagMe_Init();
void LOS_MagMe_GetField();
void LOS_MagMe_DataProcess(uint8_t* data);
void LOS_MagMe_PWROn();
void LOS_MagMe_PWROff();
void LOS_MagMe_SetFreq();
void LOS_MagMe_WE();
void LOS_MagMe_REROFF();
void LOS_MagMe_Binary();
void LOS_CAN_MagMeCallBack_Dyn(LOS_CANMSG_t* can_re);
void LOS_MagMe_GetField_Dyn();

#endif /* PERIPHERAL_LAYER_INC_LOS_MAGSEN_H_ */
